package lcmpt;

import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

public final class LCMPTTreeSelectionListener implements TreeSelectionListener {

	private JEditorPane editPane;
	private JScrollPane scrollPane;

	public LCMPTTreeSelectionListener(JEditorPane editPane,
			JScrollPane scrollPane) {
		this.editPane = editPane;
		this.scrollPane = scrollPane;
	}

	@Override
	public void valueChanged(final TreeSelectionEvent event) {
		final TreePath treePath = event.getPath();
		final Object pathComponent = treePath.getLastPathComponent();
		if (!(pathComponent instanceof MyTreeNode))
			return;
		MyTreeNode treeNode = (MyTreeNode) pathComponent;

		final String name = (String) treeNode.getUserObject();
		final String data = treeNode.dataGerman;
		String text = "<h1>" + name + "</h1>" + data + "<br>" + treeNode.marc
				+ "<br>";
		editPane.setText(text);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				scrollPane.getVerticalScrollBar().getModel().setValue(0);
			}
		});

	}
}
